package com.han.fu.api.business.controller;

import com.han.fu.api.business.model.ao.BindingAo;
import com.han.fu.api.business.model.ao.LoginAo;
import com.han.fu.api.business.model.ao.SecretAo;
import com.han.fu.api.business.model.vo.CurrentUserVo;
import com.han.fu.api.business.model.vo.LoginVo;
import com.han.fu.api.business.service.IAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 鉴权接口
 *
 * @author: star.zhu
 * @date: 2021-07-31
 */
@RestController
public class AuthController {

    @Autowired
    IAuthService service;

    /**
     * 密钥认证
     *
     * @param ao 密钥数据
     * @return LoginVo 认证数据
     */
    @PostMapping("/auth/secret")
    public LoginVo secret(@Validated @RequestBody SecretAo ao) {
        return service.secret(ao);
    }

    /**
     * 登陆认证
     *
     * @param ao 登陆数据
     * @return LoginVo 认证数据
     */
    @PostMapping("/auth/login")
    public LoginVo login(@Validated @RequestBody LoginAo ao) {
        return service.login(ao);
    }

    /**
     * 退出登陆
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/auth/logout")
    public void logout() {
        service.logout();
    }

    /**
     * 获取当前登陆用户
     *
     * @return memberInfoVo 当前用户数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @GetMapping("/auth/current/user")
    public CurrentUserVo currentUser() {
        return service.currentUser();
    }

    /**
     * 用户绑定
     *
     * @param ao 数据
     */
    @PreAuthorize("hasRole('tourist') OR hasRole('member') OR hasRole('admin')")
    @PostMapping("/user/binding")
    public LoginVo bindingUser(@Validated @RequestBody BindingAo ao) {
        return service.bindingUser(ao);
    }

}
